<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="Vue2教程-工具单文件组件介绍​        在开始的Vue项目中，我们使用 Vue.component 来定义全局组件，然后使用 new Vue({el: ‘#xx’}) 在每个页面内指定一个容器元素。  全局定义：强制要求每个 component 中的命名不重复 字符串模板：缺乏语法高亮，在HTML有多行的时候，需要用到 \ 不支持CSS：意味着当 HTML 和 JavaScript 组件">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue-使用-工具">
<meta property="og:url" content="http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E5%B7%A5%E5%85%B7/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="Vue2教程-工具单文件组件介绍​        在开始的Vue项目中，我们使用 Vue.component 来定义全局组件，然后使用 new Vue({el: ‘#xx’}) 在每个页面内指定一个容器元素。  全局定义：强制要求每个 component 中的命名不重复 字符串模板：缺乏语法高亮，在HTML有多行的时候，需要用到 \ 不支持CSS：意味着当 HTML 和 JavaScript 组件">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-29T15:15:41.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.323Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="文档">
<meta property="article:tag" content="Vue2">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E5%B7%A5%E5%85%B7/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E5%B7%A5%E5%85%B7/","path":"2021/10/29/Vue/Vue2-教程/Vue2-教程-工具/","title":"Vue-使用-工具"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Vue-使用-工具 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Vue2%E6%95%99%E7%A8%8B-%E5%B7%A5%E5%85%B7"><span class="nav-number">1.</span> <span class="nav-text">Vue2教程-工具</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6"><span class="nav-number">1.1.</span> <span class="nav-text">单文件组件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.1.1.</span> <span class="nav-text">介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%8E%E4%B9%88%E7%9C%8B%E5%BE%85%E5%85%B3%E6%B3%A8%E7%82%B9%E5%88%86%E7%A6%BB"><span class="nav-number">1.1.1.1.</span> <span class="nav-text">怎么看待关注点分离</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B5%B7%E6%AD%A5"><span class="nav-number">1.1.2.</span> <span class="nav-text">起步</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BE%8B%E5%AD%90%E6%B2%99%E7%AE%B1"><span class="nav-number">1.1.2.1.</span> <span class="nav-text">例子沙箱</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%92%88%E5%AF%B9%E5%88%9A%E6%8E%A5%E8%A7%A6JavaScript%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%94%A8%E6%88%B7"><span class="nav-number">1.1.2.2.</span> <span class="nav-text">针对刚接触JavaScript模块开发系统的用户</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%92%88%E5%AF%B9%E9%AB%98%E7%BA%A7%E7%94%A8%E6%88%B7"><span class="nav-number">1.1.2.3.</span> <span class="nav-text">针对高级用户</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95"><span class="nav-number">1.2.</span> <span class="nav-text">测试</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#TypeScript-%E6%94%AF%E6%8C%81"><span class="nav-number">1.3.</span> <span class="nav-text">TypeScript 支持</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%9F%E6%88%90%E7%8E%AF%E5%A2%83%E9%83%A8%E7%BD%B2"><span class="nav-number">1.4.</span> <span class="nav-text">生成环境部署</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%80%E5%90%AF%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E6%A8%A1%E5%BC%8F"><span class="nav-number">1.4.1.</span> <span class="nav-text">开启生产环境模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF%E9%A2%84%E7%BC%96%E8%AF%91"><span class="nav-number">1.4.2.</span> <span class="nav-text">模板预编译</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8F%90%E5%8F%96%E7%BB%84%E4%BB%B6%E7%9A%84CSS"><span class="nav-number">1.4.3.</span> <span class="nav-text">提取组件的CSS</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B7%9F%E8%B8%AA%E8%BF%90%E8%A1%8C%E6%97%B6%E9%94%99%E8%AF%AF"><span class="nav-number">1.4.4.</span> <span class="nav-text">跟踪运行时错误</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E5%B7%A5%E5%85%B7/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue-使用-工具
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-10-29 23:15:41" itemprop="dateCreated datePublished" datetime="2021-10-29T23:15:41+08:00">2021-10-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/Vue2/" itemprop="url" rel="index"><span itemprop="name">Vue2</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Vue2教程-工具"><a href="#Vue2教程-工具" class="headerlink" title="Vue2教程-工具"></a>Vue2教程-工具</h1><h2 id="单文件组件"><a href="#单文件组件" class="headerlink" title="单文件组件"></a>单文件组件</h2><h3 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h3><p>​        在开始的Vue项目中，我们使用 Vue.component 来定义全局组件，然后使用 new Vue({el: ‘#xx’}) 在每个页面内指定一个容器元素。</p>
<ul>
<li><strong>全局定义</strong>：强制要求每个 component 中的命名不重复</li>
<li><strong>字符串模板</strong>：缺乏语法高亮，在HTML有多行的时候，需要用到 \</li>
<li><strong>不支持CSS</strong>：意味着当 HTML 和 JavaScript 组件化时，CSS 明显被遗漏</li>
<li><strong>没有构建步骤</strong>：限制只能使用 HTML 和 ES5 JavaScript，而不能使用预处理器，如 Pug(formerly Jade) 和 Babel</li>
</ul>
<p>​    因此对应的 .vue 单文件组件便解决了上述的方法。并且还可以使用 webpack 和 Browserify 等构建工具。</p>
<h4 id="怎么看待关注点分离"><a href="#怎么看待关注点分离" class="headerlink" title="怎么看待关注点分离"></a>怎么看待关注点分离</h4><p>​        关注点分离不等于文件类型分离。现代的UI开发中，相比于将代码分成三大层次相互交织，我们将其划分为松散耦合的组件在将其组合起来更合理一些。</p>
<p>​        在一个组件中，模板，逻辑和样式都是内部耦合的。但是组件和组件之间时解耦的。</p>
<p>​    当然，我们也仍然可以将 JavaScript、css 分离成独立的文件然后做到热重载和预编译。</p>
<h3 id="起步"><a href="#起步" class="headerlink" title="起步"></a>起步</h3><h4 id="例子沙箱"><a href="#例子沙箱" class="headerlink" title="例子沙箱"></a>例子沙箱</h4><h4 id="针对刚接触JavaScript模块开发系统的用户"><a href="#针对刚接触JavaScript模块开发系统的用户" class="headerlink" title="针对刚接触JavaScript模块开发系统的用户"></a>针对刚接触JavaScript模块开发系统的用户</h4><h4 id="针对高级用户"><a href="#针对高级用户" class="headerlink" title="针对高级用户"></a>针对高级用户</h4><p>​        CLI 会为你搞定大多数工具的配置问题，同时也支持细粒度自定义<a target="_blank" rel="noopener" href="https://cli.vuejs.org/zh/config/">配置项</a>。</p>
<p>​        有时你会想从零搭建你自己的构建工具，这时你需要通过 <a target="_blank" rel="noopener" href="https://vue-loader.vuejs.org/zh/">Vue Loader</a> 手动配置 webpack。关于学习更多 webpack 的内容，请查阅<a target="_blank" rel="noopener" href="https://webpack.js.org/configuration/">其官方文档</a>和 <a target="_blank" rel="noopener" href="https://webpack.academy/p/the-core-concepts">Webpack Academy</a>。</p>
<p>​    分别看看上面的 cli loader webpack 来进行详细的了解。</p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><h2 id="TypeScript-支持"><a href="#TypeScript-支持" class="headerlink" title="TypeScript 支持"></a>TypeScript 支持</h2><h2 id="生成环境部署"><a href="#生成环境部署" class="headerlink" title="生成环境部署"></a>生成环境部署</h2><h3 id="开启生产环境模式"><a href="#开启生产环境模式" class="headerlink" title="开启生产环境模式"></a>开启生产环境模式</h3><h3 id="模板预编译"><a href="#模板预编译" class="headerlink" title="模板预编译"></a>模板预编译</h3><h3 id="提取组件的CSS"><a href="#提取组件的CSS" class="headerlink" title="提取组件的CSS"></a>提取组件的CSS</h3><h3 id="跟踪运行时错误"><a href="#跟踪运行时错误" class="headerlink" title="跟踪运行时错误"></a>跟踪运行时错误</h3>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"># Vue</a>
              <a href="/tags/%E6%96%87%E6%A1%A3/" rel="tag"># 文档</a>
              <a href="/tags/Vue2/" rel="tag"># Vue2</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E8%BF%87%E6%B8%A1%E5%92%8C%E5%8A%A8%E7%94%BB/" rel="prev" title="Vue-使用-过渡和动画">
                  <i class="fa fa-chevron-left"></i> Vue-使用-过渡和动画
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/29/Vue/Vue2-%E6%95%99%E7%A8%8B/Vue2-%E6%95%99%E7%A8%8B-%E8%A7%84%E6%A8%A1%E5%8C%96/" rel="next" title="Vue-使用-规模化">
                  Vue-使用-规模化 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
